class MetaController < ApplicationController
  layout "land"
  before_filter :authorize
  def index
    redirect_to :action => 'editing'
  end
  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def editing
    userid = session[:user_id]
    @meta_pages, @metas = paginate :metas, :per_page => 10, :conditions => ["user_id = ? and validate_at is null and finish_at is null", userid]
  end
  
  def list
   user_id = session[:user_id]
   @meta_pages, @metas = paginate :metas, :per_page => 10, :conditions => ["user_id = ? and validate_at is null and finish_at is not null", user_id]
  end
  
  def detail
    @meta = Meta.find(params[:id])
  end
  
  def validated_list
    userid = session[:user_id]
    @meta_pages, @metas = paginate :metas, :per_page => 10, :conditions => ["user_id = ? and validate_at is not null", userid]
  end

  def show
    @meta = Meta.find(params[:id])
  end

  def new
    @meta = Meta.new
  end

  def create
    @meta = Meta.new(params[:meta])
    @meta.user_id = session[:user_id]
    if @meta.save
      flash[:notice] = '元数据成功添加！'
      redirect_to :action => 'show', :id => @meta.id
    else
      render :action => 'new'
    end
  end

  def edit
    @meta = Meta.find(params[:id])
  end

  def update
    @meta = Meta.find(params[:id])
    if @meta.update_attributes(params[:meta])
      flash[:notice] = '元数据成功更新！'
      redirect_to :action => 'show', :id => @meta
    else
      render :action => 'edit'
    end
  end

  def destroy
    if Meta.find(params[:id]).destroy
      redirect_to :action => 'editing'
    end
  end
  
  def alternative
    if Meta.find(params[:id]).alternatives.create(params[:alternative])
    flash[:notice] = "交互题名成功添加！"
    redirect_to :action => "show", :id => params[:id]
    end
  end
  
  def editan
    @alternative = Alternative.find(params[:id])
  end
  
  def updatean
    @alternative = Alternative.find(params[:id])
    if @alternative.update_attributes(params[:alternative])
    flash[:notice] = '交互题名成功更新！'
    redirect_to :action => 'show', :id => @alternative.meta_id
    end
  end
  
  def destroyan
    metaid = Alternative.find(params[:id]).meta_id
    Alternative.find(params[:id]).destroy
    redirect_to :action => 'show', :id => metaid 
  end
  
  def create_subject_word
    if Meta.find(params[:id]).subject_words.create(params[:subject_word])
    flash[:notice] = "该主题词已添加！"
    redirect_to :action => 'show', :id => params[:id]
    end
  end
  
  def edit_subject_word
    @subject_word = SubjectWord.find(params[:id])
  end
  
  def update_subject_word
    @subject_word = SubjectWord.find(params[:id])
    if @subject_word.update_attributes(params[:subject_word])
    flash[:notice] = "该主题词已经更新！"
    redirect_to :action => 'show', :id => @subject_word.meta_id
    end
  end
  
  def destroy_subject_word
    metaid = SubjectWord.find(params[:id]).meta_id
    SubjectWord.find(params[:id]).destroy
    redirect_to :action => 'show', :id => metaid
  end
  
  def add_classification
    if Meta.find(params[:id]).classifications.create(params[:classification])
    flash[:notice] = '分类号添加成功！'
    redirect_to :action => 'show', :id => params[:id]
    end
  end
  
  def edit_classification
    @classification = Classification.find(params[:id])
  end
  
  def update_classification
    @classification = Classification.find(params[:id])
    if@classification.update_attributes(params[:classification])
    flash[:notice] = '分类号更新成功！'
    redirect_to :action => 'show', :id => @classification.meta_id
    end
  end
  
  def destroy_classification
    @classification = Classification.find(params[:id])
    metaid = @classification.meta_id
    @classification.destroy
    redirect_to :action => 'show', :id => metaid
  end
  
  def add_publisher
  if Meta.find(params[:id]).publishers.create(params[:publisher])
    flash[:notice] = "该出版者添加成功！"
    redirect_to :action => "show", :id => params[:id]
  end
  end
  
  def edit_publisher
    @publisher = Publisher.find(params[:id])
  end
  
  def update_publisher
    @publisher = Publisher.find(params[:id])
    if @publisher.update_attributes(params[:publisher])
      flash[:notice] = "该出版者信息更新成功！"
      redirect_to :action => "show", :id => @publisher.meta_id
    end
  end
  
  def destroy_publisher
    metaid = Publisher.find(params[:id]).meta_id
    Publisher.find(params[:id]).destroy
    redirect_to :action => "show", :id => metaid
  end
  
  def add_language
    if Meta.find(params[:id]).languages.create(params[:language])
    flash[:notice] = "语种代码添加成功！"
    redirect_to :action => "show", :id => params[:id]
    end
  end
  
  def edit_language
    @language = Language.find(params[:id])
  end
  
  def update_language
    @language = Language.find(params[:id])
    if @language.update_attributes(params[:language])
      flash[:notice] = "语种代码更新成功！"
      redirect_to :action => "show", :id => @language.meta_id
    end
  end
  
  def destroy_language
      @language = Language.find(params[:id])
      metaid = @language.meta_id
      @language.destroy
        redirect_to :action => "show", :id => metaid
  end
  
  def add_creator
    if Meta.find(params[:id]).creators.create(params[:creator])
      flash[:notice] = "该创作者添加成功！"
      redirect_to :action => "show", :id => params[:id]
    end
  end
  
  def edit_creator
    @creator = Creator.find(params[:id])
  end
  
  def update_creator
    @creator = Creator.find(params[:id])
    if @creator.update_attributes(params[:creator])
      flash[:notice] = "该创作者的信息已经更新！"
      redirect_to :action => "show", :id => @creator.meta_id
    end
  end
  
  def destroy_creator
    @creator = Creator.find(params[:id])
    metaid = @creator.meta_id
    @creator.destroy
    flash[:notice] = "该创作者已被删除！"
    redirect_to :action => "show", :id => metaid
  end
  
  def add_contributor
    if Meta.find(params[:id]).contributors.create(params[:contributor])
      flash[:notice] = "该辅助创作者添加成功！"
      redirect_to :action => "show", :id => params[:id]
    end
  end
  
  def edit_contributor
    @contributor = Contributor.find(params[:id])
  end
  
  def update_contributor
    @contributor = Contributor.find(params[:id])
    if @contributor.update_attributes(params[:contributor])
      flash[:notice] = "该辅助创作者的信息已经更新！"
      redirect_to :action => "show", :id => @contributor.meta_id
    end
  end
  
  def destroy_contributor
    @contributor = Contributor.find(params[:id])
    metaid = @contributor.meta_id
    @contributor.destroy
    flash[:notice] = "该辅助创作者已被删除！"
    redirect_to :action => "show", :id => metaid
  end
  
  def add_audience
    if Meta.find(params[:id]).audiences.create(params[:audience])
      flash[:notice] = "读者对象代码添加成功！"
      redirect_to :action => "show", :id => params[:id]
    end
  end
  
  def edit_audience
    @audience = Audience.find(params[:id])
  end
  
  def update_audience
    @audience = Audience.find(params[:id])
    if @audience.update_attributes(params[:audience])
    flash[:notice] = "读者对象更新成功！"
    redirect_to :action => "show", :id => @audience.meta_id
    end
  end
  
  def destroy_audience
    @audience = Audience.find(params[:id])
    metaid = @audience.meta_id
    @audience.destroy
    flash[:notice] = "该读者对象已被删除"
    redirect_to :action => "show", :id => metaid
  end
  
  def new_document
    @document = Document.new
  end
  
  def add_document
    if Meta.find(params[:id]).documents.create(params[:document])
      flash[:notice] = "文献上传成功！"
      redirect_to :action => "show", :id => params[:id]
    else
      render :action => 'new_document'
    end 
  end
  
  def edit_document
    @document = Document.find(params[:id])
  end
  
  def update_document
    @document = Document.find(params[:id])
    if @document.update_attributes(params[:document])
      flash[:notice] = "更新成功！"
      redirect_to :action => "show", :id => @document.meta_id
    end
  end
  
  def destroy_document
    @document = Document.find(params[:id])
    metaid = @document.meta_id
    @document.destroy
    flash[:notice] = "该文献已被删除！"
    redirect_to :action => "show", :id => metaid
  end
  
  def add_identifier
    if Meta.find(params[:id]).identifiers.create(params[:identifier])
    flash[:notice] = "唯一标识码添加成功！"
    redirect_to :action => "show", :id => params[:id]
    end
  end
  
  def edit_identifier
    @identifier = Identifier.find(params[:id])
  end
  
  def update_identifier
  @identifier = Identifier.find(params[:id])
    if @identifier.update_attributes(params[:identifier])
    flash[:notice] = "唯一标识码更新成功！"
    redirect_to :action => "show", :id => @identifier.meta_id
    end
  end
  
  def destroy_identifier
    @identifier = Identifier.find(params[:id])
    metaid = @identifier.meta_id
    if @identifier.destroy
      flash[:notice] = "该唯一标识码已被删除！"
      redirect_to :action => "show", :id => metaid
    end
  end
  
  def new_meta
    @meta = Meta.new
  end
  
  def add_meta
  @meta =  Meta.new(params[:meta])
  @meta.user_id = session[:user_id]
  @meta.parent_id = params[:id]
    if @meta.save
      flash[:notice] = "子集添加成功！"
      redirect_to :action => "show", :id => params[:id]
    end
  end
  
  def add_citation
    if Meta.find(params[:id]).citations.create(params[:citation])
      flash[:notice] = "引文添加成功！"
      redirect_to :action => "show", :id => params[:id]
    end
  end
  
  def edit_citation
    @citation = Citation.find(params[:id])
  end
  
  def update_citation
  @citation = Citation.find(params[:id])
    if @citation.update_attributes(params[:citation])
    flash[:notice] = "引文信息更新成功！"
    redirect_to :action => "show", :id => @citation.meta_id
    end
  end
  
  def destroy_citation
    @citation = Citation.find(params[:id])
    metaid = @citation.meta_id
    if @citation.destroy
      flash[:notice] = "该引文已被删除！"
      redirect_to :action => "show", :id => metaid
    end
  end
  
  def show_versions
    @versions = Version.find_by_version_doc_id(params[:id])
  end
  
  def add_version
    if Meta.find(params[:id]).versions.create(params[:version])
      flash[:notice] = "版本关联成功！"
      redirect_to :action => "show", :id => params[:id]
    end
  end
  
  def table_of_contents
    @tocs = TableOfContent.find(:all,
                                :conditions => ["meta_id = ? and parent_id = 0", params[:id]])
  end
  
  def add_table_of_content
    if Meta.find(params[:id]).table_of_contents.create(params[:table_of_content])
      flash[:notice] = "该顶级目录标题添加成功！"
      redirect_to :action => "table_of_contents", :id => params[:id]
    end
  end
  
  def edit_table_of_content
    @table_of_content = TableOfContent.find(params[:id])
  end
  
  def update_table_of_content
    @table_of_content = TableOfContent.find(params[:id])
    if @table_of_content.update_attributes(params[:table_of_content])
      flash[:notice] = "更新成功！"
      if @table_of_content.parent_id ==0
        redirect_to :action => "table_of_contents", :id => @table_of_content.meta_id
      else
        redirect_to :action => "table_of_content", :id => @table_of_content.parent_id
      end
    end
  end
  
  def destroy_table_of_content
    @toc = TableOfContent.find(params[:id])
    metaid = @toc.meta_id
    parentid = @toc.parent_id
    if @toc.destroy
      flash[:notice] = "删除成功！"
      if parentid == 0
        redirect_to :action => "table_of_contents", :id => metaid
      else
        if TableOfContent.find(:all, :conditions => ["parent_id = ?",parentid]).size == 0
          flash[:notice] = "该目录已没有子目录！"
        end
        redirect_to :action => "table_of_content", :id => parentid
      end
    end
  end
  
  def new_sub_toc
    @metaid = TableOfContent.find(params[:id]).meta_id
  end
  
  def add_sub_toc
  @toc = TableOfContent.find(params[:id])
  params[:meta_id] = @toc.meta_id
    if @toc.children.create(params[:table_of_content])
      flash[:notice] = "子目录添加成功！"
      if @toc.parent_id == 0
        redirect_to :action => "table_of_contents", :id => @toc.meta_id
      else
        redirect_to :action => "table_of_content", :id => @toc
      end
    end
  end
  
  def table_of_content
    @sub_tocs = TableOfContent.find(:all, :conditions => ["parent_id = ?",params[:id]])
  end
  
  def add_replace
    if Meta.find(params[:id]).replaces.create(params[:replace])
      flash[:notice] = "文献替换信息添加成功！"
      redirect_to :action => "show", :id => params[:id]
    end
  end
  
  def show_replace
    @replace = Replace.find(params[:id])
  end
  
  def edit_replace
    @replace = Replace.find(params[:id])
  end
  
  def update_replace
    @replace = Replace.find(params[:id])
    if @replace.update_attributes(params[:replace])
      flash[:notice] = "信息更新成功！"
      redirect_to :action => "show_replace", :id => @replace
    end
  end
  
  def destroy_replace
    @replace = Replace.find(params[:id])
    metaid = @replace.meta_id
    if @replace.destroy
      flash[:notice] = "文献替换信息删除成功！"
      redirect_to :action => "show", :id => metaid
    end
  end
  
  def add_date
    if Meta.find(params[:id]).dats.create(params[:dat])
      flash[:notice] = "日期添加成功！"
      redirect_to :action => "show", :id => params[:id]
    end
  end
  
  def edit_date 
    @dat = Dat.find(params[:id])
  end
  
  def update_date
    @dat = Dat.find(params[:id])
    if @dat.update_attributes(params[:dat])
      flash[:notice] = "信息更新成功！"
      redirect_to :action => "show", :id => @dat.meta_id
    end
  end
  
  def destroy_date
    @dat = Dat.find(params[:id])
    metaid = @dat.meta_id
    if @dat.destroy
      flash[:notice] = "该日期已被删除！"
      redirect_to :action => "show", :id => metaid
    end
  end
  
  def add_location
    if Meta.find(params[:id]).locations.create(params[:location])
      flash[:notice] = "馆藏信息添加成功！"
      redirect_to :action => "show", :id => params[:id]
    end
  end
  
  def edit_location
    @location = Location.find(params[:id])
  end
  
  def update_location
    @location = Location.find(params[:id])
    if @location.update_attributes(params[:location])
      flash[:notice] = "馆藏信息更新成功！"
      redirect_to :action => "show", :id => @location.meta_id
    end
  end
  
  def destroy_location
    @location = Location.find(params[:id])
    metaid = @location.meta_id
    if @location.destroy
      flash[:notice] = "馆藏信息已被删除！"
      redirect_to :action => "show", :id => metaid
    end
  end
  
  def add_snapshot
    if Meta.find(params[:id]).snapshots.create(params[:snapshot])
      flash[:notice] = "快照上传成功！"
      redirect_to :action => "show", :id => params[:id]
    end
  end
  
  def snapshot
    @snapshot = Snapshot.find(params[:id])
    send_data(@snapshot.data,
              :filename => @snapshot.name,
              :type => @snapshot.content_type,
              :disposition => "inline")
  end
  
  def destroy_snapshot
    @snapshot = Snapshot.find(params[:id])
    metaid = @snapshot.meta_id
    if @snapshot.destroy
      flash[:notice] = "该快照已被删除！"
      redirect_to :action => "show", :id => metaid
    end
  end
  
  def finish
    meta = Meta.find(params[:id])
    if meta.update_attribute(:finish_at, Time.now)
      redirect_to :action => "list"
    end
  end
  
  def unfinish
    meta = Meta.find(params[:id])
    if meta.update_attribute(:finish_at, nil)
      redirect_to :action => "list"
    end
  end
end 
